<?php

/**
 * @package     Joomla.Platform
 * @subpackage  Form
 *
 * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */
defined('JPATH_PLATFORM') or die;

/**
 * Form Field class for the Joomla Platform.
 * Provides an input field for files
 *
 * @package     Joomla.Platform
 * @subpackage  Form
 * @link        http://www.w3.org/TR/html-markup/input.file.html#input.file
 * @since       11.1
 */
class JFormFieldCustomFile extends JFormField
{

    /**
     * The form field type.
     *
     * @var    string
     * @since  11.1
     */
    public $type = 'CustomFile';

    /**
     * Method to get the field input markup for the file field.
     * Field attributes allow specification of a maximum file size and a string
     * of accepted file extensions.
     *
     * @return  string  The field input markup.
     *
     * @since   11.1
     *
     * @note    The field does not include an upload mechanism.
     * @see     JFormFieldMedia
     */
    protected function getInput()
    {
        require_once JPATH_PLUGINS . '/manage/files/defines.php';
        if (file_exists(JPATH_COMPONENT_ADMINISTRATOR . "/defines.php")) {
            require_once JPATH_COMPONENT_ADMINISTRATOR . "/defines.php";
        } else if (file_exists(JPATH_COMPONENT . "/defines.php")) {
            require_once JPATH_COMPONENT_SITE . "/defines.php";
        }
        // Initialize some field attributes.
        $getInput = array();
        $getInput['accept'] = $this->element['accept'] ? $this->element['accept'] : '';
        $getInput['size'] = $this->element['size'] ? $this->element['size'] : '';
        $getInput['class'] = $this->element['class'] ? $this->element['class'] : '';
        $getInput['directory'] = $this->element['directory'] ? $this->element['directory'] : '';
        $getInput['width'] = $this->element['resize_width'] ? $this->element['resize_width'] : '';
        $getInput['height'] = $this->element['resize_height'] ? $this->element['resize_height'] : '';
        $getInput['fileExt'] = $this->element['file_ext'] ? $this->element['file_ext'] : '';
        $getInput['fileNote'] = $this->element['filenote'] ? $this->element['filenote'] : '';
        $getInput['disabled'] = ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
        $getInput['onchange'] = $this->element['onchange'] ? $this->element['onchange'] : '';
        $getInput['encodeName'] = $this->element['encode_name'] ? $this->element['encode_name'] : 0;
        $getInput['resizeDirectory'] = $this->element['resize_directory'] ? $this->element['resize_directory'] : "";
        foreach ($getInput as $key => &$val) {
            $val = isset($$val) ? $$val : $val;
        }
        $getInput['accept'] = $getInput['accept'] ? ' accept="' . (string) $getInput['accept'] . '"' : '';
        $getInput['size'] = $getInput['size'] ? ' size="' . (int) $getInput['size'] . '"' : '';
        $getInput['class'] = $getInput['class'] ? ' class="' . (string) $getInput['class'] . '"' : 'class=""';
        $getInput['onchange'] = $getInput['onchange'] ? ' onchange="' . (string) $getInput['onchange'] . '"' : '';

        $name = substr($this->name, 0, -1);
        $main_directory = "images";

        // Initialize JavaScript field attributes.
        $com_params = JComponentHelper::getParams("com_media");
        $allowable = explode(',', $com_params->get('upload_extensions'));
        $img_ext_allow = explode(',', $com_params->get('image_extensions'));
        $plgFiles = JPluginHelper::getPlugin("manage", "files");
        $registry = new JRegistry;
        if (is_object($plgFiles)) {
            if (isset($plgFiles->params) && $plgFiles->params) {
                $loadString = @$registry->loadString($plgFiles->params);
                if ($loadString) {
                    $plgFiles->params = $registry->toArray();
                    if (isset($plgFiles->params['main_directory']) && $plgFiles->params['main_directory']) {
                        $main_directory = $plgFiles->params['main_directory'];
                    }
                }
            }
            $input['file'] = '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' value=""' . $getInput['accept'] . $getInput['disabled'] . $getInput['class'] . $getInput['size'] . $getInput['onchange'] . ' />';
//        $input['delete'] = '<input type="checkbox" name="jform[delete][' . $this->group .'][' . $this->fieldname . ']" value="' . "{$main_directory}/{$getInput['directory']}/{$this->value}";
            $input['filenote'] = "<br /><span>{$getInput['fileNote']}</span>\n";
            $value = explode(".", $this->value);
            if (isset($this->value) && $this->value && is_array($value) && count($value) && $value) {
                $value = end($value);
                if (in_array($value, $allowable)) {
                    if (in_array($value, $img_ext_allow)) {
                        $input['image'] = "<img src=" . JUri::root() . "{$main_directory}/{$getInput['directory']}/$this->value /><br />";
                    } else {
                        $input['image'] = $this->value;
                    }
                }
            }
            if ($getInput['resizeDirectory']) {
                $input['resize_directory'] = "<input type='hidden' name='{$name}_resize_directory]' value='{$main_directory}/{$getInput['resizeDirectory']}' />";
//            $input['delete'] .= "||{$main_directory}/{$getInput['resizeDirectory']}/{$this->value}";
            }
//        $input['delete'] .= '" />';
            $input['directory'] = "<input type='hidden' name='{$name}_directory]' value='{$main_directory}/{$getInput['directory']}' />";
            $input['size'] = "<input type='hidden' name='{$name}_width]' value='{$getInput['width']}' />
                          <input type='hidden' name='{$name}_height]' value='{$getInput['height']}' />";
            $input['ext'] = "<input type='hidden' name='{$name}_ext]' value='{$getInput['fileExt']}' /> ";
            $input['value'] = "<input type='hidden' name='{$name}_field_value]' value='{$this->value}' /> ";
            $input['encode'] = "<input type='hidden' name='{$name}_encode_name]' value='{$getInput['encodeName']}' /> ";
            $input = implode("\n", $input);
            return $input;
        } else {
            return "Cannot load plugin: Files";
        }
    }

}
